package at.fhv.androideightpuzzle.activities;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import at.fhv.androideightpuzzle.R;
import at.fhv.androideightpuzzle.application.MultiPlayerHostController;
import at.fhv.androideightpuzzle.application.SelectPuzzleController;
import at.fhv.androideightpuzzle.application.interfaces.ISelectPuzzleController;
import at.fhv.androideightpuzzle.puzzles.PuzzleLoader;

public class SelectPuzzleActivity extends Activity implements Observer,OnItemClickListener{
	
	public static final int PLAY_PUZZLE_DIALOG=0;
	
	private ISelectPuzzleController m_controller;
	private String[] m_puzzleIds;
	private int[] m_puzzleImageIds;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.puzzle_list_item);
		
		//logic to get the appropriate controller
		if(getIntent().getBooleanExtra(IntentExtraKeys.IS_MULTI_PLAYER_HOST,false)==true){
			m_controller=MultiPlayerHostController.getInstance();
			((MultiPlayerHostController)m_controller).addObserver(this);
		}else{
			m_controller=new SelectPuzzleController();
		}
		
		m_puzzleIds=m_controller.getPuzzleIds();
		m_puzzleImageIds=new int[m_puzzleIds.length];
		
		for(int i=0; i<m_puzzleImageIds.length; i++){
			m_puzzleImageIds[i]=PuzzleLoader.loadImageIdForPuzzle(m_puzzleIds[i]);
		}
		
		setContentView(R.layout.puzzle_list);
		
		ListView listView=(ListView)findViewById(R.id.puzzle_list);
		//listView.setAdapter(new ArrayAdapter<String>(this,R.layout.puzzle_list_item,m_puzzleIds));
		listView.setAdapter(new PuzzleItemAdapter(this));
		listView.setOnItemClickListener(this);
	}
	
	@Override
	public void onStart(){
		super.onStart();
	}
	
	private void startToPlay(String puzzleId){
		if(puzzleId!=null){
			Intent intent=new Intent(this,PlayPuzzleActivity.class);
			intent.putExtra(IntentExtraKeys.PUZZLE_ID,puzzleId);
			intent.putExtra(IntentExtraKeys.IS_MULTI_PLAYER_HOST,getIntent().getBooleanExtra(IntentExtraKeys.IS_MULTI_PLAYER_HOST,false));
			intent.putExtra(IntentExtraKeys.IS_MULTI_PLAYER_CLIENT,getIntent().getBooleanExtra(IntentExtraKeys.IS_MULTI_PLAYER_CLIENT,false));
			
			startActivity(intent);
		}
	}
	
	public void onItemClick(AdapterView<?> parent,View view,int position,long id){
		if((position>=0) && (position<m_puzzleIds.length)){
			m_controller.selectPuzzle(position);
			
			startToPlay(m_puzzleIds[position]);
		}
	}
	
	public class PuzzleItemAdapter extends BaseAdapter{
		
		private Context m_context;
		
		public PuzzleItemAdapter(Context context){
			m_context=context;
		}
		
		public int getCount(){
			return m_puzzleImageIds.length;
		}

		public Object getItem(int position){
			return null;
		}

		public long getItemId(int position){
			return 0;
		}

		public View getView(int position,View convertView,ViewGroup parent){
			ImageView imageView=null;
			
			if(convertView==null){
				imageView=new ImageView(m_context);
				
				imageView.setPadding(5,5,5,5);
				imageView.setAdjustViewBounds(true);
				imageView.setMaxWidth(120);
				imageView.setMaxHeight(120);
				imageView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,AbsListView.LayoutParams.WRAP_CONTENT));
			}else{
				imageView=(ImageView)convertView;
			}
			
			imageView.setImageResource(m_puzzleImageIds[position]);
			
			return imageView;
		}
	}
	
	public void update(Observable observable, Object argument){
	}
}
